package com.rongji.dfish.demo.editor.service.uiadaptor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.demo.constant.DemoConstants;
import com.rongji.dfish.demo.entity.DfishViewNode;
import com.rongji.dfish.framework.FrameworkHelper;
import com.rongji.dfish.ui.Command;
import com.rongji.dfish.ui.Layout;
import com.rongji.dfish.ui.Widget;
import com.rongji.dfish.ui.command.ConfirmCommand;
import com.rongji.dfish.ui.command.DialogCommand;
import com.rongji.dfish.ui.form.AbstractBoxgroup;
import com.rongji.dfish.ui.form.AbstractUpload;
import com.rongji.dfish.ui.form.Period;
import com.rongji.dfish.ui.form.Pickbox;
import com.rongji.dfish.ui.form.Validatable;
import com.rongji.dfish.ui.form.Validate;
import com.rongji.dfish.ui.helper.FlexGrid;
import com.rongji.dfish.ui.helper.FormPanel;
import com.rongji.dfish.ui.helper.GridLayoutFormPanel;
import com.rongji.dfish.ui.helper.GridPanel;
import com.rongji.dfish.ui.helper.StackPanel;
import com.rongji.dfish.ui.helper.TabPanel;
import com.rongji.dfish.ui.layout.GridLayout;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.widget.DialogTemplate;
import com.rongji.dfish.ui.widget.Leaf;
import com.rongji.dfish.ui.widget.Split;

public class JavaCodeBuilder{
	JavaCodeBuilder(){}
	// 一行允许最大的方法数量
	protected static final int MAX_METHODS_PER_LINE = 2;

	

	/**
	 * 根据视图节点列表组装Java编码
	 * @param viewNodes 视图节点列表
	 * @return Java编码
	 */
	public String getJavaCode(List<DfishViewNode> viewNodes) {
		if (Utils.isEmpty(viewNodes)) {
			return "";
		}
		Map<String, List<DfishViewNode>> mapping = new HashMap<String, List<DfishViewNode>>();
		Map<String, String> typeMapping = new HashMap<String, String>();
		for (DfishViewNode viewNode : viewNodes) {
			List<DfishViewNode> nodes = mapping.get(viewNode.getNodeParent());
			if (nodes == null) {
				nodes = new ArrayList<DfishViewNode>();
				mapping.put(viewNode.getNodeParent(), nodes);
			}
			nodes.add(viewNode);
			typeMapping.put(viewNode.getNodeId(), viewNode.getNodeType());
		}
		StringBuilder javaCode = new StringBuilder();
		Map<String, Integer> context = new HashMap<String, Integer>();
		Map<String, String> variables = new HashMap<String, String>();
		fillSubNodeCascade(javaCode, DemoConstants.ID_ROOT, null, context, variables, mapping, typeMapping);
		return javaCode.toString();
	}
	
	/**
	 * 递归，填充下级视图节点的Java编码，形成一整段完整代码。
	 * 
	 * @param javaCode 已生成的Java编码
	 * @param pId 父节点的编号
	 * @param parent 父对象
	 * @param context 已存在的类型变量的下标
	 * @param variables 视图编号对应的变量名称
	 * @param mapping 父编号对应其子节点的映射
	 * @param typeMapping 视图节点编号对应的节点类型
	 */
	private void fillSubNodeCascade(StringBuilder javaCode, String pId, Class<?> parent, Map<String, Integer> context, Map<String, String> variables, Map<String, List<DfishViewNode>> mapping, Map<String, String> typeMapping) {
		List<DfishViewNode> subNodes = mapping.get(pId);
		if (subNodes == null) {
			return;
		}
		String pType = typeMapping.get(pId);
		for (DfishViewNode selfNode : subNodes) {
			TypeAdaptor adaptor = AdaptorManager.getInstance().getCompInfo().getAdaptor(selfNode.getNodeType());
			if (adaptor == null) {
				continue;
			}
			String currNode = selfNode.getNodeId();
			if (Utils.isEmpty(pType) || Utils.notEmpty(mapping.get(currNode))) {
//				if ((Utils.isEmpty(pType) || Utils.notEmpty(mapping.get(currNode)))&&!"Checkboxgroup".equals(pType)) {
				// 无父对象或有子对象时
				String currType = selfNode.getNodeType();
				Integer count = context.get(currType);
				String variable = currType.substring(0, 1).toLowerCase() + currType.substring(1);
				if (count != null && count > 0) {
					count++;
					variable += count;
				} else {
					count = 1;
				}
				context.put(currType, count);
				variables.put(currNode, variable);
			}
			
			buildInstanceFull(javaCode, variables, parent, adaptor, selfNode);
			Class<?>selfClz =adaptor.getClass();
			if(adaptor instanceof ComponentTypeAdaptor){
				ComponentTypeAdaptor cast=(ComponentTypeAdaptor)adaptor;
				selfClz=cast.getClz();
			}
			fillSubNodeCascade(javaCode, selfNode.getNodeId(), selfClz, context, variables, mapping, typeMapping);
		}
	}

	/**
	 * 获取Java代码
	 * 这里代码包含完整代码
	 * (1)新建实例 (2)设置属性 (3)添加到父节点去
	 * 但并不含递归。
	 * @param javaCode 代码将被压入该StringBuilder
	 * @param variables 已命名的变量映射关系。nodeId, 变量名
	 * @param parent 父节点类型
	 * @param adaptor 当前实例的TypeAdaptor
	 * @param viewNode 当前实例的信息
	 */
	protected void buildInstanceFull(StringBuilder javaCode,Map<String, String> variables,Class<?> parent, TypeAdaptor adaptor,DfishViewNode viewNode
		    ) {
		ComponentData data = AdaptorManager. parseData(viewNode);
		String variable = variables.get(viewNode.getNodeId());
		int beginLeng=javaCode.length();
		if (parent==null) {
			adaptor.getInstanceCode(javaCode, data,variable);
		} else {
//			List<DfishViewNode> subNodes = mapping.get(pId);
			String pVariable = variables.get(viewNode.getNodeParent());
			append2Parent(javaCode, data, parent,pVariable, adaptor,variable);
		}
		if (javaCode.length()>beginLeng) {
			javaCode.append(';');
		}
		javaCode.append("\r\n\r\n");
	}

	/**
	 * 填充到父对象的Java代码
	 * @param javaCode 
	 * @param data
	 * @param parent 
	 * @param pVariable
	 * @param adaptor 
	 * @param variable
	 */
	protected void append2Parent(StringBuilder javaCode, ComponentData data, Class<?> parent,String pVariable, TypeAdaptor adaptor,String variable) {
		if (GridLayoutFormPanel.class.isAssignableFrom(parent)) {
			String methodName = "add";
			//FIXME 空
			Map<String, String> parentProps = data.getParentProps();
			Integer fromRow = AdaptorManager.parseInt(parentProps.get("fromRow"));
			Integer fromColumn = AdaptorManager.parseInt(parentProps.get("fromColumn"));
			Integer toRow = AdaptorManager.parseInt(parentProps.get("toRow"));
			Integer toColumn = AdaptorManager.parseInt(parentProps.get("toColumn"));
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable + "." + methodName + "(");
			} else {
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append(";\r\n" + pVariable + "." + methodName + "(");
			}
			if (toRow!=null && toColumn !=null) {
				javaCode.append(fromRow + ", " + fromColumn + ", " + toRow + ", " + toColumn + ", ");
			} else {
				javaCode.append(fromRow + ", " + fromColumn + ", " );
			}
			if (Utils.isEmpty(variable)) {
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append( ")");
			} 
		}else if((StackPanel.class).isAssignableFrom(parent)||(TabPanel.class).isAssignableFrom(parent)){
			Map<String, String> parentProps = data.getParentProps();
			String methodName = "addSubWidget";
			String title=parentProps.get("title")==null?"":parentProps.get("title");
			String icon=parentProps.get("icon")==null?"":parentProps.get("icon");
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable + "." + methodName + "(");
				javaCode.append("\""+icon + "\",\"" + title + "\", " );
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append( ")");
			} else {
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append(";\r\n" + pVariable + "." + methodName + "(");
				javaCode.append("\""+icon + "\",\"" + title + "\","+variable+")" );
			}
			
		}else if((AbstractUpload.class).isAssignableFrom(parent)){
			String mode = data.getMode();
			String methodName="";
			boolean upload_button="[upload_button]".equals(mode);
			if(upload_button){
				methodName="addUpload_button";
				javaCode.append(pVariable).append('.').append(methodName).append('(');
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(')');
			}
		}else if((AbstractBoxgroup.class).isAssignableFrom(parent)){
			String mode = data.getMode();
			String methodName="";
			boolean target="[target]".equals(mode);
			if(target){
				methodName="addTarget";
				//若有子对象，则要new当前对象，并将variable添加到父对象中
				if(Utils.notEmpty(variable)){
					adaptor.getInstanceCode(javaCode, data,variable);
					javaCode.append(";\r\n");
					javaCode.append(pVariable).append('.').append(methodName).append('(');
					javaCode.append(variable);
					javaCode.append(')');
					return;
				}
			}else{
				methodName="add";
			}
			javaCode.append(pVariable).append('.').append(methodName).append('(');
			adaptor.getInstanceCode(javaCode, data,variable);
			javaCode.append(')');
		}else if((View.class).isAssignableFrom(parent)){
			Class<?> self=adaptor.getClass();
			if(adaptor instanceof ComponentTypeAdaptor){
				ComponentTypeAdaptor cast=(ComponentTypeAdaptor)adaptor;
				self=cast.getClz();
			}
			String methodName = getAppend2parentMethodName(parent, self);
			if(Utils.isEmpty(variable)){
				javaCode.append(pVariable + "." + methodName + "(");
			}else{
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append(";\r\n" + pVariable + "." + methodName + "(");
			}
			//View内添加Command、DialogTemplate时需要id参数
			if((Command.class).isAssignableFrom(self) || (DialogTemplate.class).isAssignableFrom(self)){
				String id = "\"" + data.getParentProps().get("id") + "\"";
				javaCode.append(id + ", ");
			}
			if(Utils.isEmpty(variable)){
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append(")");
			}else{
				javaCode.append(variable).append(')');
			}
			
		}else if((Pickbox.class).isAssignableFrom(parent)){
			//Pickbox视图
			Class<?> self=adaptor.getClass();
			if(adaptor instanceof ComponentTypeAdaptor){
				ComponentTypeAdaptor cast=(ComponentTypeAdaptor)adaptor;
				self=cast.getClz();
			}
			String methodName = getAppend2parentMethodName(parent, self);
			if((DialogCommand.class).isAssignableFrom(self)){
				methodName = "setPicker";
			}
			if(Utils.isEmpty(variable)){
				javaCode.append(pVariable + "." + methodName + "(");
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append(")");
			}else{
				adaptor.getInstanceCode(javaCode, data, variable);
				javaCode.append(";\r\n" + pVariable + "." + methodName + "(");
				javaCode.append(variable).append(')');
			}
		}else if((ConfirmCommand.class).isAssignableFrom(parent)){
			String methodName = "";
			if("<yes>".equals(data.getMode())){
				methodName="setYes";
			}else if("<no>".equals(data.getMode())){
				methodName="setNo";
			}
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable).append('.').append(methodName).append('(');
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(')');
			} else {
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(";\r\n");
				javaCode.append(pVariable).append('.').append(methodName).append('(').append(variable).append(')');
			}
			
		}else if((FlexGrid.class).isAssignableFrom(parent)){
			// 初始化占用FlexGrid个数的代码
			String occupy= data.getParentProps()!=null?data.getParentProps().get("occupy"):null;
			Class<?> self=adaptor.getClass();
			if(adaptor instanceof ComponentTypeAdaptor){
				ComponentTypeAdaptor cast=(ComponentTypeAdaptor)adaptor;
				self=cast.getClz();
			}
			String methodName = getAppend2parentMethodName(parent, self);
			javaCode.append(pVariable).append('.').append(methodName).append('(');
			adaptor.getInstanceCode(javaCode, data,variable);
			if (Utils.notEmpty(occupy)) {
				javaCode.append(',').append(occupy);
			}
			javaCode.append(')');
		}else if((GridPanel.class).isAssignableFrom(parent)){
			String methodName = "add";
			if("[column]".equals(data.getMode())){
				methodName="addColumn";
			}else if("<pub>".equals(data.getMode())){
				methodName="setPub";
			}
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable).append('.').append(methodName).append('(');
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(')');
			} else {
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(";\r\n");
				javaCode.append(pVariable).append('.').append(methodName).append('(').append(variable).append(')');
			}
		}else if((GridLayout.class).isAssignableFrom(parent)){
			String methodName = "add";
			if("<thead>".equals(data.getMode())){
				methodName="setThead";
			}else if("<pub>".equals(data.getMode())){
				methodName="setPub";
			}else if("[column]".equals(data.getMode())){
				methodName="addColumn";
			}
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable).append('.').append(methodName).append('(');
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(')');
			} else {
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(";\r\n");
				javaCode.append(pVariable).append('.').append(methodName).append('(').append(variable).append(')');
			}
		}else if((Leaf.class).isAssignableFrom(parent)){
			String methodName = "add";
			if("<box>".equals(data.getMode())){
				methodName="setBox";
			}
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable).append('.').append(methodName).append('(');
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(')');
			} else {
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(";\r\n");
				javaCode.append(pVariable).append('.').append(methodName).append('(').append(variable).append(')');
			}
		}else if((Period.class).isAssignableFrom(parent)){
			String methodName = "";
			if("<begin>".equals(data.getMode())){
				methodName="setBegin";
			}else if("<end>".equals(data.getMode())){
				methodName="setEnd";
			}
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable).append('.').append(methodName).append('(');
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(')');
			} else {
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(";\r\n");
				javaCode.append(pVariable).append('.').append(methodName).append('(').append(variable).append(')');
			}
			
		}else{
			Class<?> self=adaptor.getClass();
			if(adaptor instanceof ComponentTypeAdaptor){
				ComponentTypeAdaptor cast=(ComponentTypeAdaptor)adaptor;
				self=cast.getClz();
			}
			if (parent == null) {
				// 理论上不会抛出这个异常
				throw new IllegalArgumentException("父对象为空");
			}
			String methodName = getAppend2parentMethodName(parent, self);

			
//			if(data.getProps()!=null&&"<pub>".equals(data.getProps().get("id"))){
//				methodName="setPub";
//			}
			if("<pub>".equals(data.getMode())){
				methodName="setPub";
			}else if("<split>".equals(data.getMode())){
				methodName="setSplit";
			}
			
			if (Utils.isEmpty(variable)) {
				javaCode.append(pVariable).append('.').append(methodName).append('(');
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(')');
			} else {
				adaptor.getInstanceCode(javaCode, data,variable);
				javaCode.append(";\r\n");
				javaCode.append(pVariable).append('.').append(methodName).append('(').append(variable).append(')');
			}
		}
	}
	/**
	 * 父对象添加当前对象的方法名
	 * @param parent 父对象
	 * @param self 当前对象
	 * @return
	 * FIXME 判断极其混论性能不高
	 */
	protected String getAppend2parentMethodName(Class<?> parent, Class<?> self) {
		// FIXME 目前没有考虑全部情况
		String methodName = "add";
		if ( View.class.isAssignableFrom(parent)) {
			if (DialogTemplate.class.isAssignableFrom(self)) {
				//必须先判断DialogTemplate，否则会被Widget的判断截止
				methodName = "addTemplate";
			} else if ( Widget.class.isAssignableFrom(self)) {
				methodName = "add";
			} else if (Command.class.isAssignableFrom(self)) {
				methodName = "addCommand";
			} else {
				// FIXME 其他情况待补充
			}
//		} else if (StackPanel.class.isAssignableFrom(parent)||TabPanel.class.isAssignableFrom(parent)) {
//			methodName = "addSubWidget";
		} else if (Layout.class.isAssignableFrom(parent)) {
			methodName = "add";
		} else if (FormPanel.class==parent) {
			methodName = "add";
		}else if(Validatable.class.isAssignableFrom(parent)){
			if (Validate.class.isAssignableFrom(self)) {
				methodName = "addValidate";
			}
		} 
		return methodName;
	}
	/**
	 * 拼接基本属性set方法的Java代码 
	 * 需要把扩展属性Data Hidden Event的代码加上
	 * @param sb 拼接的字符串
	 * @param props 所有基本属性
	 * @param propDefines 属性定义
	 * @param br 是否换行
	 * @param type 所对应的组件类型 比如：DatePicker
	 */
	private static void fillProps(StringBuilder sb, String type, Map<String, PropertyDefine> propDefines, Map<String, String> props, boolean br ) {
		if(Utils.isEmpty(props)){
			return;
		}
		for (Map.Entry<String, String> entry : props.entrySet()) {
			if (br) {
				// 换行以及代码制表符收缩
				sb.append('\r').append('\n').append('\t');
			}
			PropertyDefine propDef = propDefines.get(entry.getKey());
			if (propDef == null) {
				continue;
			}
			String propType = propDef.getType();
			String value = entry.getValue();
			String prop = entry.getKey();
			if("id".equals(prop)){//特殊属性进行屏蔽
				if(value!=null&&value.startsWith("<")){
					continue;
				}
			}
			sb.append(".set").append(prop.substring(0, 1).toUpperCase()).append(prop.substring(1)).append("(");
			try {
				toArg(sb,type,BeanExtUtil.convert(propType),prop,value);
			} catch (ClassNotFoundException e) {
				sb.append("null");
				FrameworkHelper.LOG.error(null, e);
			}
			sb.append(")");
		}
	}
	private static void fillEventProps(StringBuilder sb, String typeName, Map<String, String> props, boolean br) {
		
		if(Utils.isEmpty(props)){
			return;
		}
		for (Map.Entry<String, String> entry : props.entrySet()) {
			if (br) {
				// 换行以及代码制表符收缩
				sb.append('\r').append('\n').append('\t');
			}
			
			String value = entry.getValue();
			value=value.replace("\"", "'");
			String prop = entry.getKey();
			sb.append(".setOn(");
			List<Object[]> allKeys=AdaptorManager.getInstance().getCompInfo().getConstants(ComponentInfo.ELEM_TYPE_EVENT);
			String cons=getConstant4Code(prop,allKeys);
			if(Utils.isEmpty(cons)){
				sb.append('"');
				deepEscape(sb,prop);
				sb.append('"');
			}else{
				sb.append(typeName).append(".").append(cons);
			}
			sb.append(", \"");
			deepEscape(sb,value);
			sb.append("\")");
		}
	}
	private static void fillExtProps(StringBuilder sb, Map<String, String> props,String methodName, boolean br) {
		if(Utils.isEmpty(props)){
			return;
		}
		for (Map.Entry<String, String> entry : props.entrySet()) {
			if (br) {
				// 换行以及代码制表符收缩
				sb.append('\r').append('\n').append('\t');
			}
			
			String value = entry.getValue();
			String prop = entry.getKey();
			sb.append('.');
			sb.append(methodName);
			sb.append("(\"");
			deepEscape(sb,prop);
			sb.append("\",\"");
			deepEscape(sb,value);
			sb.append("\")");
		}
	}
	private static void fillDataProps(StringBuilder sb, Map<String, String> props, boolean br) {
		fillExtProps(sb, props, "setData", br);
	}
	private static void fillHiddensProps(StringBuilder sb, Map<String, String> props, boolean br) {
		fillExtProps(sb, props, "addHidden", br);
	}

	private static void deepEscape(StringBuilder sb, String value) {
		// TODO Auto-generated method stub
		sb.append(value);
	}

	/**
	 * 获取初始化实例代码,
	 * 并且加入其它属性的设置
	 * 但不包含子属性的设置
	 * @param javaCode 
	 * @param data 
	 * @param adaptor 
	 */
	public static void buildInstanceWithProps(StringBuilder javaCode,ComponentData data,TypeAdaptor adaptor) {
		// 方法数量科
		Map<String, String> props = data.getProps();
		Map<String, String> hiddensProps = data.getHiddensProps();
		Map<String, String> eventProps = data.getEventProps();
		Map<String, String> dataProps = data.getDataProps();
		 //判断是否文件加载的组件
		adaptor= AdaptorManager.getInstance().getCompInfo().getComponentDefine((ComponentTypeAdaptor) adaptor);
		Map<String, PropertyDefine> propDefines = adaptor.getDefinePropMap();
		
		int methodCount = (props==null?0:props.size())+
				 (hiddensProps==null?0:hiddensProps.size())+
				 (eventProps==null?0:eventProps.size())+
				 (dataProps==null?0:dataProps.size());

		boolean br = methodCount > MAX_METHODS_PER_LINE;
		String type=adaptor.getTypeName();
		fillProps(javaCode , type, propDefines,props,  br);
		fillHiddensProps(javaCode, hiddensProps, br);
		fillDataProps(javaCode, dataProps, br);
		fillEventProps(javaCode,type, eventProps, br);
	}
	/**
	 * 
	 * @param argStr 传入的值，比如：yyyy-mm-dd
	 * @param clz 传入的类型 
	 * @param sb 
	 * @param type 传入的组件名称，比如：DatePicker
	 * @param argName 传入组件当前属性，比如：value
	 */
	public static void toArg(StringBuilder sb, String type, Class<?> clz, String argName,String argStr) {
		if (argStr == null ) {
			sb.append("null");
		} else if (Number.class.isAssignableFrom(clz)) {
			//Spinner生成代码初始化的类型为NUMBER,有值为数值，没有值为null
			sb.append(Utils.notEmpty(argStr)? argStr:"null");
		} else if (clz == int.class || clz == long.class|| clz == double.class ||  clz == float.class ||clz == short.class) {
			sb.append(argStr);
		} else if (clz == Boolean.class || clz == boolean.class) {
			sb.append("1".equals(argStr));
		} else if (Collection.class.isAssignableFrom(clz) ) {
			//Radiogroup、Checkboxgroup代码初始化的时候设置options
			argStr=argStr.replace("[", "{").replace("]", "}");
			sb.append(Utils.notEmpty(argStr)?"Arrays.asList(new Object[][]" + argStr + ")":"null");
		} else if(clz == Object.class&&argStr.startsWith("[\"")&&argStr.endsWith("\"]")){// 表单值如：["1","4"]
			argStr=argStr.replace("[", "{").replace("]", "}");
			sb.append("Arrays.asList(new Object[]" + argStr + ")");
		} else if(clz == Split.class){
			sb.append("new Split().setWidth("+argStr+")");
		} else if(clz == String.class){
			//传进来的字符串用常量表示
			Object elemType=getPropCodeType(type, argName);
			if(elemType!=null){
				List<Object[]> allKeys=AdaptorManager.getInstance().getCompInfo().getConstants(elemType);
				String constant=getConstant4Code(argStr,allKeys);
				if(Utils.notEmpty(constant)){
					sb.append(type+"." + constant);				
				}else{
					sb.append(argStr);
				}
			}else{
				sb.append('"');
				sb.append(argStr.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n").replace("\"", "\\\""));
				sb.append('"');
			}
		}else {// FIXME 可能有其他类型如Date
			sb.append('"');
			sb.append(argStr.replace("\\", "\\\\").replace("\r", "\\r").replace("\n", "\\n").replace("\"", "\\\""));
			sb.append('"');
			FrameworkHelper.LOG.debug("===其他类型:"+clz+"===组件名："+type+"===属性名:"+argName+"=====");
		}
	}
	/**
	 * 判断字符串是否需要获取常量，返回标识
	 * @param type 传入的组件名称，比如：DatePicker
	 * @param argName 传入组件当前属性，比如：value
	 * @return
	 */
//	private static Object getPropCodeType(String type,String argName){
//		Object elemType=null;
//		// FIXME 有其他常量还没有判断
//		if("DatePicker".equals(type)&&"format".equals(argName)){
//			elemType=ComponentInfo.ELEM_TYPE_DATE_FORMAT;
//		}else if("align".equals(argName)){
//			elemType=ComponentInfo.ELEM_TYPE_ALIGN;
//		}else if("valign".equals(argName)){
//			elemType=ComponentInfo.ELEM_TYPE_VALIGN;
//		}else if("scroll".equals(argName)){
//			elemType=ComponentInfo.ELEM_TYPE_SCROLL;
//		}else if("DialogTemplate".equals(type)||"DialogCommand".equals(type)){
//			if("width".equals(argName)){
//				elemType=ComponentInfo.ELEM_TYPE_TYPE_DIALOGWIDTH;
//			}else if("height".equals(argName)){
//				elemType=ComponentInfo.ELEM_TYPE_TYPE_DIALOGHEIGHT;
//			}
//		}else if("validateeffect".equals(argName)){
//			elemType=ComponentInfo.ELEM_TYPE_TYPE_SUBMITCOMMAND;
//		}else if("face".equals(argName)){
//			elemType=ComponentInfo.ELEM_TYPE_TYPE_GRIDLAYOUT;
//		}else if("Validate".equals(type)&&"pattern".equals(argName)){
//			elemType=ComponentInfo.ELEM_TYPE_TYPE_PATTERN;
//		}
//		return elemType;
//	}
	static final HashMap<String, Object> PROP_CODETYPE_MAP=new HashMap<String, Object>();
	static{
		// FIXME 可能有其他常量还没有判断
		initCodePropType("", null);
		initCodePropType("DatePicker", "format" ,ComponentInfo.ELEM_TYPE_DATE_FORMAT);
		initCodePropType("MuldatePicker", "format" ,ComponentInfo.ELEM_TYPE_DATE_FORMAT);
		initCodePropType("align", ComponentInfo.ELEM_TYPE_ALIGN);
		initCodePropType("valign", ComponentInfo.ELEM_TYPE_VALIGN);
		initCodePropType("scrollClass", ComponentInfo.ELEM_TYPE_SCROLLCLASS);
		initCodePropType("DialogTemplate", "width", ComponentInfo.ELEM_TYPE_TYPE_DIALOGWIDTH);
		initCodePropType("DialogTemplate", "height", ComponentInfo.ELEM_TYPE_TYPE_DIALOGHEIGHT);
		initCodePropType("DialogCommand", "width", ComponentInfo.ELEM_TYPE_TYPE_DIALOGWIDTH);
		initCodePropType("DialogCommand", "height", ComponentInfo.ELEM_TYPE_TYPE_DIALOGHEIGHT);
		initCodePropType("ButtonBar", "dir", ComponentInfo.ELEM_TYPE_TYPE_BUTTONBAR);
		initCodePropType("validateeffect", ComponentInfo.ELEM_TYPE_TYPE_SUBMITCOMMAND);
		initCodePropType("face", ComponentInfo.ELEM_TYPE_TYPE_GRIDLAYOUT);
		initCodePropType("Validate", "pattern", ComponentInfo.ELEM_TYPE_TYPE_PATTERN);
	}
	private static final void initCodePropType(String argName,Object elemType){
		PROP_CODETYPE_MAP.put(argName, elemType);
	}
	private static final void initCodePropType(String type,String argName,Object elemType){
		if(type!=null&&argName.equals("")){
			initCodePropType(argName,elemType);
		}else{
			initCodePropType(type+"("+argName+")",elemType);
		}
	}
	public static Object getPropCodeType(String type,String argName){
		String key=type+"("+argName+")";
		Object result=PROP_CODETYPE_MAP.get(key);
		if(result!=null){
			return result;
		}
		key=argName;
		result=PROP_CODETYPE_MAP.get(key);
		if(result!=null){
			return result;
		}
		result=PROP_CODETYPE_MAP.get("");

		return result;
	}
	/**
	 * 获取拼接的常量
	 * @param argStr 传入的String值：yyyy-mm-dd
	 * @param contants 对应的常量list
	 * @return
	 */
	private static String getConstant4Code(String argStr,List<Object[]> contants) {
		if (Utils.isEmpty(argStr)) {
			return "";
		}
		for (Object[] sData : contants) {
			if (argStr.equals(sData[0])) {
				return (String) sData[1];
			}
		}
		return "";
	}
	
}
